package my2Cents;

import java.io.Serializable;
import java.util.Vector;

import net.phys2d.raw.Body;
import net.phys2d.raw.BodyList;
import net.phys2d.raw.World;

public class BodyStateManager implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -707099279412411861L;
	
	private Vector<Vector<BodyState>> bodyStates = new Vector<Vector<BodyState>>();
	private Vector<Body> excluded = new Vector<Body>();
	
	public void merge(BodyStateManager other) {
		for(int i=0; i<other.bodyStates.size(); i++) {
			Vector<BodyState> states = other.bodyStates.get(i);
			this.bodyStates.set(i, states);
		}
	}
	
	public void excludeBody(Body b) {
		excluded.add(b);
	}
	
	public void reIncludeBody(Body b) {
		if(excluded.contains(b)) {
			excluded.remove(b);
		}
	}
	
	public boolean isExcluded(Body b) {
		return excluded.contains(b);
	}
	
	public Vector<Vector<BodyState>> getBodyStates() {
		return bodyStates;
	}
	
	public void initBodyStateVector(int index) {
		System.out.println("Initialized[" + index + "]!");
		bodyStates.add(index,new Vector<BodyState>());
	}
	
	public boolean bodyStored(int index, Body b) {
		Vector<BodyState> states = bodyStates.get(index);
		for(int i=0; i<states.size(); i++) {
			BodyState state = (BodyState) states.get(i);
			if(state.getBody() == b) {
				return true;
			}
		}
		return false;
	}
	
	public void purgeBodyState(int index, Body b) {
		Vector<BodyState> states = bodyStates.get(index);
		for(int i=0; i<states.size(); i++) {
			BodyState state = (BodyState) states.get(i);
			if(state.getBody() == b && !excluded.contains(b)) {
				states.remove(state);
			}
		}
	}
	
	public void saveSingleBodyState(int index, World w, Body body) {
		BodyState state = new BodyState(body);
		w.clearArbiters(state.getBody());
		bodyStates.get(index).add(state);
	}
	
	public void saveBodyStates(int index, World w) {
		BodyList bodies = w.getBodies();
		Vector<BodyState> states = new Vector<BodyState>();
		for(int i=0; i<bodies.size(); i++) {
			Body body = bodies.get(i);
			boolean cancel = excluded.contains(body);
			if(!cancel) {
				BodyState state = new BodyState(body);
				w.clearArbiters(state.getBody());
				states.add(state);
			}
		}
		bodyStates.set(index, states);
	}
	
	public void restoreBodyStates(int index, World w) {
		BodyList bodies = w.getBodies();
		Vector<BodyState> states = bodyStates.get(index);
		if(states != null) {
			for(int x=0; x<states.size(); x++) {
				BodyState state = (BodyState)states.get(x);
				boolean found = false;
				for(int y=0; y<bodies.size(); y++) {
					Body body = bodies.get(y);
					if(body == state.getBody()) {
						if(!excluded.contains(body)) {
							state.resolveBody(body);
						}
						found = true;
					}
				}
				if(!found) {
					Body b = state.getNewBody();
					w.add(b);
				}
			}
		} else {
			System.err.println("No BodyState List At Given Index");
		}
	}
}
